<?php defined('SYSPATH') OR die('No direct access allowed.'); ?>

<script type="text/javascript">
$(document).ready(function() {

	$("#btnSearch").click(function() {
		var q = $("#txtCriteria").attr('value');
		if (q==='') {
			alert('กรุณาระบุคำค้นหา');
			return false;
		} else {
			var c = parseInt($('[name=rdoCategory]').fieldValue());
			var uri = ISS.lang.getBasePath()+"index.php/search/song_hits/page/" + q + "/" + c + "/1";
			window.location = uri;
		}
	}); 

	$(".actionPlay").click(function() {
		var uri = $(this).attr('href'); 
		if (uri==='') {
			alert('ไม่มีข้อมูลเพลง');
		}
		else {
			ISS.Popup.Windows.Show_Modal("View Song", uri, null, 280, 420, null);		
		}
		return false;
	});
	
	$(".actionDownload").click(function() {
		var uri = $(this).attr('href'); 
		if (uri==='') {
			alert('กรุณา sign in เข้าใช้งานระบบก่อนทำการดาวน์โหลดเพลง');
		}
		else {
			ISS.Ajax.Caller.request(uri, null);	
		}
		return false;
	});

	$(".actionVote").click(function() {
		var uri = $(this).attr('href'); 
		if (uri==='') {
			alert('กรุณา sign in เข้าใช้งานระบบก่อนทำการโหวตเพลง');
		}
		else {
			ISS.Popup.Windows.Show_Modal("Vote Song", uri, null, 350, 520, null);		
		}
		return false;
	});
	
}); 
</script>

<?php 	
	echo html::Stylesheet(
		array
		(
			'media/css/search_block.css',
		),
		array
		(
			'screen',
		)
	);	
?>

<div id="mainCont">
	<div id="criteria">
		<form id="frmSearch" name="frmSearch" action="#" method="get">
			<table>
				<tr>
					<td colspan="3">
						<h2>กรุณาระบุคำค้นหา</h2>
					</td>
				</tr>
				<tr>
					<td>ชื่อเพลงฮิต</td>
					<td><input type="text" id="txtCriteria" name="txtCriteria" value="<?php echo html::specialchars($form['txtCriteria']); ?>"></input></td>
					<td><input type="button" id="btnSearch" name="btnSearch" value="ค้นหา"></input></td>
				</tr>
				<tr>
					<td>จัดหมวดหมู่ความนิยม</td>
					<td colspan="2">
					<input type="radio" name="rdoCategory" value="1" <?php echo ($form['rdoCategory']=='1') ? 'checked' : ''; ?>>มากไปน้อย</input>
					<input type="radio" name="rdoCategory" value="2" <?php echo ($form['rdoCategory']=='2') ? 'checked' : ''; ?>>น้อยไปมาก</input>
					</td>
				</tr>
			</table>
		</form>
	</div>
	<?php 	
		if (empty($result)) {
			echo '<h2>ไม่พบรายการค้นหา</h2>';
		}
		else {
			echo '<h2>รายการค้นหา</h2>';
			$header_title='';
			$body_open = false;
			$i=0;
			foreach ($result as $row) {
				$is_changed = false;
				
				if ($header_title <> $row['rating']) {
					$is_changed = true;
				}
				
				if ($is_changed) {
					
					if ($body_open) {
						echo '</table>';
						echo '</div>';
						$body_open = false;	
					}
					$i=1;

					echo '<div id="searchHeader">';
						$header_title = $row['rating'];
						echo '<h3>จำนวนความยอดนิยม :: '. $header_title .'</h3>';					
					echo '</div>';
					
					$body_open = true; 
					?>					
					<div id="searchBody">
					<table>
						<tr>
							<td class="header_seq">ลำดับ</td>
							<td class="header_album">อัลบัม</td>
							<td class="header_track">เพลงที่</td>
							<td class="header_song">ชื่อเพลง</td>
							<td class="header_artist">ศิลปิน</td>
							<td class="header_gene">แนวเพลง</td>
							<td class="header_play">ฟัง</td>
							<td class="header_download">ดาวน์โหลด</td>
							<td class="header_vote">โหวต</td>
						</tr>
						<?php echo (($i % 2)==0) ? '<tr class="searchListDark">' : '<tr class="searchListLight">'; ?>
							<td class="body_seq"><?php echo $i; ?></td>
							<td class="body_album"><?php echo html::specialchars($row['album_name']); ?></td>
							<td class="body_track"><?php echo html::specialchars($row['track_no']); ?></td>
							<td class="body_song"><?php echo html::specialchars($row['title']); ?></td>
							<td class="body_artist"><?php echo html::specialchars($row['artist_name']); ?></td>
							<td class="body_gene"><?php echo html::specialchars($row['gene']); ?></td>
							<td class="body_play">
				        		<a class='actionPlay' href="<?php echo url::base() . 'index.php/top_order/show/show_song/'. $row['album_id'] .'/'. $row['track_no']; ?>">
				        		<img class="imgPlay" alt="ฟัง" src="<?php echo url::base();?>/media/images/play_icon.png"/></a>
							</td>
							<td class="body_download">
				        		<?php echo number_format($row['downloaded']); ?>&nbsp;<a class='actionDownload' href="<?php if (!empty($form['profile_id'])) { echo url::base() . '/index.php/vote/song/ajax_download/' . $form['profile_id'] . '/' . $row['album_id'] .'/' . $row['track_no']; } ?>" target="_blank" ><img class="imgDownload" alt="ดาวน์โหลด" src="<?php echo url::base();?>/media/images/download_icon.png"/></a>
							</td>
							<td class="body_vote">
				        		<?php echo number_format($row['voted']); ?>&nbsp;<a class='actionVote' href="<?php if (!empty($form['profile_id'])) { echo url::base() . '/index.php/vote/song/request_vote/' . $form['profile_id'] . '/' . $row['album_id'] .'/' . $row['track_no']; } ?>">
				        		<img class="imgVote" alt="โหวต" src="<?php echo url::base();?>/media/images/vote_icon.png"/></a>
							</td>
						</tr>
					<?php 					
				}
				else {
					$i++;
					echo (($i % 2)==0) ? '<tr class="searchListDark">' : '<tr class="searchListLight">'; ?>
							<td class="body_seq"><?php echo $i; ?></td>
							<td class="body_album"><?php echo html::specialchars($row['album_name']); ?></td>
							<td class="body_track"><?php echo html::specialchars($row['track_no']); ?></td>
							<td class="body_song"><?php echo html::specialchars($row['title']); ?></td>
							<td class="body_artist"><?php echo html::specialchars($row['artist_name']); ?></td>
							<td class="body_gene"><?php echo html::specialchars($row['gene']); ?></td>
							<td class="body_play">
				        		<a class='actionPlay' href="<?php echo url::base() . 'index.php/top_order/show/show_song/'. $row['album_id'] .'/'. $row['track_no']; ?>">
				        		<img class="imgPlay" alt="ฟัง" src="<?php echo url::base();?>/media/images/play_icon.png"/></a>
							</td>
							<td class="body_download">
				        		<?php echo number_format($row['downloaded']); ?>&nbsp;<a class='actionDownload' href="<?php if (!empty($form['profile_id'])) { echo url::base() . '/index.php/vote/song/ajax_download/' . $form['profile_id'] . '/' . $row['album_id'] .'/' . $row['track_no']; } ?>" target="_blank" ><img class="imgDownload" alt="ดาวน์โหลด" src="<?php echo url::base();?>/media/images/download_icon.png"/></a>
							</td>
							<td class="body_vote">
				        		<?php echo number_format($row['voted']); ?>&nbsp;<a class='actionVote' href="<?php if (!empty($form['profile_id'])) { echo url::base() . '/index.php/vote/song/request_vote/' . $form['profile_id'] . '/' . $row['album_id'] .'/' . $row['track_no']; } ?>">
				        		<img class="imgVote" alt="โหวต" src="<?php echo url::base();?>/media/images/vote_icon.png"/></a>
							</td>
						</tr>				
				<?php 						
				} 
			}
			if ($body_open) {
				echo '</table>';
				echo '</div>';
				$body_open = false;	
			}
		}
	
	?>
	<div id = "searchFooter">
		<?php echo $pagination; ?>
	</div>
</div>